<?php

/**
 *
 */
namespace Application\Controller;

use Application\Service\LoginService;
use Application\Utils\Captcha;
use Infobird\Controller\BaseController;
use Infobird\Utils\GuidUtils;
use Infobird\Utils\HttpUtils;
use Infobird\Utils\LogUtils;
use Infobird\Utils\RedisUtils;

class LoginController extends BaseController
{
    /**
     * 功能：1、登录页面显示
     */
    public function indexAction(){

    }

    /**
     * 功能：1、检查登录
     *       2、成功登录把登录信息写入redis
     *       3、redis有效期为12小时
     */
    public function loginAction(){
        $user_name = isset($_REQUEST['userName'])?$_REQUEST['userName']:"";
        $password = isset($_REQUEST['password'])?$_REQUEST['password']:"";
        $user = new LoginService();
        $password = md5($password);
        try{
            $result = $user->checkLogin($user_name,$password);
            //print_r($result);die;
            if($result){
                $guid = new GuidUtils();
                $token = $guid->create_guid();
                $arr['user_name'] = $user_name;
                $arr['password'] = $password;
                $redis =  RedisUtils::getRedis();
                $re = $redis->set("user_info_".$token, json_encode($arr));
                $redis->setTimeout("user_info_".$token, 60*60*24*1);
                $redis->close();
                if($re){
                    $this->setCookie("userinfo", "user_info_".$token, 60*60*12*1);
                    echo json_encode(array("result"=>"0",'token' => $token,"msg"=>"login ok"));
                    exit;
                }else {
                    echo json_encode(array("result"=>"-1","msg"=>"set redis error"));
                    exit;
                }
            }else {
                echo json_encode(array("result"=>"-2","msg"=>"user error"));
                exit;
            }
        }catch (\Exception $e){
            LogUtils::log("t_login.log", 'login'.$e->getMessage(),array("result"=>"2000"));
            exit;
        }
    }

    /**
     * 功能：1、退出登录
     *       2、清除cookie
     *       3、删除redis中的账号信息
     */
    public function loginOutAction(){
        $userinfo = $this->currentUser();
        if(!isset($userinfo)){
            echo json_encode(array("result"=>"500"));
            LogUtils::log('hd_loginout.log', '用户不存在',array("result"=>"500"));
            exit;
        }
        $cookie = $this->getCookie("userinfo");
        if($cookie) {
            setcookie("userinfo", "", time() - (60*60*24*1), '/');
            LogUtils::log('hd_loginout.log', 'cookie清除成功',array("result"=>"200"));
            //清除redis
            $redis = RedisUtils::getRedis();
            $key = "user_info_".$userinfo['token'];
            $redis->del($key);
            $redis->close();
            header('Location:/application/index/index');
            exit;
        }else {
            echo json_encode(array("result"=>"500"));
            LogUtils::log('hd_loginout.log', '用户不存在',array("result"=>"500"));
            header('Location:/application/index/index');
            exit;
        }

    }

    /**
     * 功能：1、创建验证码
     */
    public function getCodeAction(){
        $cap = new Captcha();
        $cap->create();
        exit;
    }

    /**
     * 功能：1、验证验证码是否正确
     */
    public function checkCodeAction(){
        if(!isset($_REQUEST['captcha'])||empty($_REQUEST['captcha'])){
            echo json_encode(array("result"=>"2","msg"=>"no captcha"));
            exit;
        }
        $code = $_REQUEST['captcha'];
        $captcha = new Captcha();
        $codeResult = $captcha->check($code);
        if($codeResult){
            echo "true";
            exit;
        }else{
            echo "false";
            exit;
        }
    }
}